home *** CD-ROM | disk | FTP | other *** search
- Path: paranoia.com!avinash
- From: avinash@paranoia.com (Avinash Chopde)
- Newsgroups: comp.lang.c
- Subject: Fastest way to computer log(base2) of x?
- Date: 24 Jan 1996 19:33:49 GMT
- Organization: None.
- Message-ID: <4e61iu$p6e@villa.fc.net>
- NNTP-Posting-Host: primus.paranoia.com
-
- [And no, this is not for a homework exercise.]
- [BTW, the non-abridged C FAW at http://www.eskimo.com/~scs/C-faq.top.html
- does not seem to be accessible??]
-
- I am trying to find out what could be the fastest way to compute
- the position of the highest bit in any given integer -- basically, the
- logarithm to the base 2 of any number.
-
- Simplistically, one could do :
- logbase2(int x)
- {
- if (IS_SET_BIT_31(x)) return 31;
- if (IS_SET_BIT_30(x)) return 30;
- etc.
- }
-
- An improvement would be to check for BITS_31_16 and BITS_15_0 at first, and
- then check for BITS_31_24, and BITS_23_16, etc .. (divide problem in half
- at each stage).
-
- Any thoughts or other ideas?
-
- Thanks!
-
- --
- ----
- Avinash Chopde
- e-mail: avinash@acm.org
- home page: http://www.paranoia.com/~avinash/
-